package com.allwees.bs.task.product;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.allwees.bs.module.pms.entity.Product;
import com.allwees.bs.module.pms.entity.Sku;
import com.allwees.bs.module.pms.service.BaseProductService;
import com.allwees.bs.module.pms.service.SkuService;
import com.allwees.core.common.constant.EStatus;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Daniel
 * @Version 1.0.0
 * @Description
 * @CreateTime 2020/10/9 16:18
 */
@Slf4j
@Service
public class CheckProdutPhotoTask extends S3Processor{

    @Resource
    private BaseProductService baseProductService;

    @Resource
    private SkuService skuService;

    @XxlJob(value = "checkProdutPhotoTask")
    public ReturnT<String> run(String s) {
        log.info(">>> CheckProdutPhotoTask param string:{}",s);
        if(StringUtils.isBlank(s)){
            return ReturnT.SUCCESS;
        }
        JSONObject json = JSON.parseObject(s);
        super.init(json);
        doTask(json);
        return ReturnT.SUCCESS;
    }

    private void doTask(JSONObject json){
        LambdaQueryChainWrapper<Product> wrapper = baseProductService
                .lambdaQuery()
                .select(Product::getId, Product::getUuid,Product::getRetailerUuid,Product::getMainPhoto,Product::getPhotosStr,Product::getCreatedAt);

        wrapper.ge(json.containsKey("id"),Product::getId, json.getIntValue("id"));
        wrapper.eq(Product::getStatus, EStatus.NORMAL.value());
        wrapper.and(w -> {
            w.or(w1 -> {
                w1.isNotNull(Product::getMainPhoto);
                w1.ne(Product::getMainPhoto, "");
                w1.notLike(Product::getMainPhoto, domainName);
            });

            w.or(w2 -> {
                w2.isNotNull(Product::getPhotosStr);
                w2.ne(Product::getPhotosStr, "");
                w2.notLike(Product::getPhotosStr, domainName);
            });
        });

        List<Product> products = wrapper.list();
        if (CollectionUtils.isEmpty(products)) {
            return;
        }

        log.info(">>> CheckProdutPhotoTask got product products size:{}", products.size());
        for (Product product : products) {
            boolean updated = false;
            Product update = new Product();
            update.setId(product.getId());

            String mainPhoto = product.getMainPhoto();
            if (StringUtils.isNotBlank(mainPhoto)) {
                mainPhoto = putObject("spu",product.getRetailerUuid(), mainPhoto, product.getCreatedAt());
                if (StringUtils.isNotBlank(mainPhoto)) {
                    update.setMainPhoto(mainPhoto);
                    updated = true;
                }
            }

            String[] photoss = product.getPhotos();
            if (photoss.length > 0) {
                List<String> photos = new ArrayList<>();
                for (String photo : photoss) {
                    if (photo.contains(domainName)) {
                        photos.add(photo);
                        continue;
                    }
                    String url = putObject("spu",product.getRetailerUuid(), photo, product.getCreatedAt());
                    if (StringUtils.isNotBlank(url)) {
                        photos.add(url);
                        updated = true;
                    }
                }
                update.setPhotosStr(StringUtils.join(photos, ","));
            }

            if (updated) {
                baseProductService.updateById(update);
            }

            List<Sku> skus = skuService.lambdaQuery()
                    .select(Sku::getId, Sku::getPhotosStr)
                    .isNotNull(Sku::getPhotosStr)
                    .ne(Sku::getPhotosStr,"")
                    .eq(Sku::getProductUuid, product.getUuid()).list();
            for (Sku sku : skus) {
                String skuImg = sku.getPhotosStr();
                if(skuImg.contains(domainName)){
                    continue;
                }
                String url = putObject("spu",product.getRetailerUuid(), skuImg, product.getCreatedAt());
                if (StringUtils.isNotBlank(url)) {
                    sku.setPhotosStr(url);
                    skuService.updateById(sku);
                }
            }
        }
    }
}
